﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using KS.DataWrapper.Interface;
using WebApiHostTest.Models;

namespace WebApiHostTest.Controllers
{
    public class TestItemClassRepository : IBaseRepository<TestItemClass>
    {
        public readonly List<TestItemClass> list = new List<TestItemClass>();

        /// <summary>
        /// Gets all.
        /// </summary>
        /// <returns></returns>
        public IQueryable<TestItemClass> GetAll()
        {
            // TODO: Implement this method
            return this.list.AsQueryable();
        }

        public object GetAllsss(Expression exp)
        {
          
            // TODO: Implement this method
            //hmmmmm 
            return list.AsQueryable().Provider.Execute(exp);
        }

        /// <summary>
        /// Gets the by.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <returns></returns>
        public TestItemClass GetBy(Expression<Func<TestItemClass, bool>> filter)
        {
            // TODO: Implement this method
            if (filter == null)
            {
                throw new ArgumentNullException("filter");
            }

            TestItemClass entity = this.GetAll().SingleOrDefault(filter);
            if (entity == null)
            {
                return default(TestItemClass);
            }

            return entity;
        }

        /// <summary>
        /// Adds the new.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public TestItemClass AddNew(TestItemClass entity)
        {
            Random rnd = new Random(System.DateTime.Now.Second);
            entity.ID = rnd.Next().ToString();
            this.list.Add(entity);
            return entity;
        }

        /// <summary>
        /// Adds the range new.
        /// </summary>
        /// <param name="entityList">The entity list.</param>
        /// <returns></returns>
        public IEnumerable<TestItemClass> AddRangeNew(IEnumerable<TestItemClass> entityList)
        {
            // TODO: Implement this method
            throw new NotImplementedException();
        }

        /// <summary>
        /// Updates the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public TestItemClass Update(TestItemClass entity)
        {
            this.list.Where(w => w.ID == entity.ID).First().SomePropery = entity.SomePropery;
            return entity;
        }

        /// <summary>
        /// Deletes the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public bool Delete(TestItemClass entity)
        {
            return this.list.Remove(entity);
        }

        /// <summary>
        /// Dynamics the linq.
        /// </summary>
        /// <param name="linq">The linq.</param>
        /// <returns></returns>
        object IBaseRepository<TestItemClass>.DynamicLinq(string linq)
        {
            // TODO: Implement this method
            throw new NotImplementedException();
        }

        public long Count()
        {
            return list.Count;
        }

        public virtual object DynamicLinq(string linq)
        {
            var collection = list;

            IQueryable<TestItemClass> queryableData = collection.AsQueryable<TestItemClass>();
            var externals = new Dictionary<string, object>();
            externals.Add(typeof(TestItemClass).Name, queryableData);

            string query = "Products.Where(w => w.OrderID > 3)";
            query = linq;
            var expression = System.Linq.Dynamic.DynamicExpression.Parse(typeof(IQueryable<TestItemClass>), query, new[] { externals });

            var result = queryableData.Provider.CreateQuery<TestItemClass>(expression);
            return result;
        }
    }
}